# -*- coding: utf-8 -*-
import re
from openerp.osv import fields, osv
from openerp.tools.translate import _

class stock_move(osv.osv):
    _inherit = "stock.move"
    
    def action_done(self, cr, uid, ids, context=None):
        #没有价格不能确认(客供料除外)
        sale_move_ids_no_price = self.search(cr, uid, [('id','in',ids), '|',('price_order','=',0),('price_order','=',False),('picking_id.oem_material','=',False),
                                                       '|',('sale_line_id','!=',False),('picking_id.sale_id','!=',False),
                                                       ],context=context)
        if sale_move_ids_no_price:
            prod_codes = '\n'.join(['%s , %s'%(move.product_id.default_code,move.product_id.part_no_external) for move in self.browse(cr, uid, sale_move_ids_no_price, context=context)])
            raise osv.except_osv('错误', '发现无价格的产品, 不能执行销售出库:\n%s'%(prod_codes))
        return super(stock_move,self).action_done(cr,uid,ids,context)
    
#johnw, 2016/12/31, for performance issue
#override code in stock and dmp_stock_inventory    
def fill_inventory_fsbc(self, cr, uid, ids, context=None):
    """ To Import stock inventory according to products available in the selected locations.
    @param self: The object pointer.
    @param cr: A database cursor
    @param uid: ID of the user currently logged in
    @param ids: the ID or list of IDs if we want more than one
    @param context: A standard dictionary
    @return:
    """
    if context is None:
        context = {}

    inventory_obj = self.pool.get('stock.inventory')
    location_obj = self.pool.get('stock.location')
    inv_line_obj = self.pool.get('stock.inventory.line')
    if ids and len(ids):
        ids = ids[0]
    else:
        return {'type': 'ir.actions.act_window_close'}
    fill_inventory = self.browse(cr, uid, ids, context=context)
    if fill_inventory.recursive:
        location_ids = location_obj.search(cr, uid, [('location_id',
                         'child_of', [fill_inventory.location_id.id])], order="id",
                         context=context)
    else:
        location_ids = [fill_inventory.location_id.id]

    inventory_id = context['active_ids'][0]
    inventory = inventory_obj.browse(cr, uid, inventory_id, context=context)
    for location_id in location_ids:
        #删除旧数据, 不知为何下面SQL数据量大时,执行时间特别长,可能是有trigger之类
        #cr.execute('delete from stock_inventory_line where inventory_id = %s and location_id = %s',(inventory_id, location_id))
        inv_line_ids_unlink = inv_line_obj.search(cr, uid, [('inventory_id','=',inventory_id),('location_id','=',location_id)],context=context)
        while inv_line_ids_unlink:
            lines_unlink = inv_line_ids_unlink[:100]
            del inv_line_ids_unlink[:100]
            inv_line_obj.unlink(cr, uid, lines_unlink, context=context)
            cr.commit()
        #插入新数据
        cr.execute('''
            insert into stock_inventory_line
            (inventory_id, location_id, product_id, product_uom,product_qty, 
            qty_onhand, pos, stock_user_id, company_id,create_date,create_uid,
            categ_id)
            select %s, %s, a.product_id, c.uom_id, 
            sum(case when a.location_dest_id=%s then coalesce(a.product_qty,0) else -coalesce(a.product_qty,0) end) as product_qty,
            sum(case when a.location_dest_id=%s then coalesce(a.product_qty,0) else -coalesce(a.product_qty,0) end) as qty_onhand,
            b.loc_pos_code, min(b.stock_user_id), %s,now(),%s,
            min(c.categ_id)
            from stock_move a
            join product_product b on a.product_id = b.id
            join product_template c on b.product_tmpl_id = c.id
            where (a.location_id = %s or a.location_dest_id = %s )
            and a.location_id != a.location_dest_id
            and a.state = 'done'
            and a.company_id = %s
            and a.date <= %s
            group by a.product_id,c.uom_id,b.loc_pos_code  ''', 
            (inventory_id, location_id, location_id, location_id, inventory.company_id.id, uid, location_id, location_id, inventory.company_id.id, inventory.date))
        if fill_inventory.set_stock_zero:
            cr.execute('update stock_inventory_line set product_qty=0 where inventory_id=%s and location_id=%s',(inventory_id, location_id))

    return {'type': 'ir.actions.act_window_close'}
#from openerp.addons.stock.wizard.stock_fill_inventory import stock_fill_inventory as stock_fill_inventory_super
from openerp.addons.dmp_stock_counting.wizard.stock_fill_inventory import stock_fill_inventory as stock_fill_inventory_super
stock_fill_inventory_super.fill_inventory = fill_inventory_fsbc        